package models;

import com.et.ar.ActiveRecordBase;
import com.et.ar.annotations.BelongsTo;
import com.et.ar.annotations.Column;
import com.et.ar.annotations.Id;
import com.et.ar.annotations.Table;
import com.et.ar.annotations.Unique;

import java.sql.*;

import utils.Md5Util;


@Table(name="userlogin")
public class Userlogin extends ActiveRecordBase{
	@Id private Integer id;
	@Column private String loginid;
	@Column private String loginpwd;
	@Column private String loginip;
	@Column private Timestamp logindate;
	@Column private Timestamp activetime;

	public static void AddUserLogin(String userid, String userip , String md5pwd) throws Exception {
		Userlogin userlogin = new Userlogin();
		userlogin.setLogindate(new Timestamp(System.currentTimeMillis()));
		userlogin.setActivetime(new Timestamp(System.currentTimeMillis()));
		userlogin.setLoginid(userid);
		userlogin.setLoginip(userip);
		userlogin.setLoginpwd(md5pwd);		
		userlogin.save();
	}
	
	public static Timestamp getUserActiveTime(String userid) throws Exception{
		Userlogin userlogin = Userlogin.findFirst(Userlogin.class,"loginid = ? " ,new Object[]{userid},"id desc");
		if (userlogin != null)
			return userlogin.getActivetime();
		else 
			return null;

	}
	
	public static void keepUserLogin(String userid,String userip) throws Exception {
		Userlogin userlogin = Userlogin.findFirst(Userlogin.class,"loginid = ? " ,new Object[]{userid},"id desc");
		if (userlogin != null){
		userlogin.setActivetime(new Timestamp(System.currentTimeMillis()));
		userlogin.setLoginip(userip);
		userlogin.save();
		}
	}
	//	@BelongsTo(foreignKey="demoId")
	//	private Demo demo;

	//	public Demo getDemo() {
	//		return demo;
	//	}
	//	public void setDemo(Demo demo) {
	//		this.demo = demo;
	//	}

	//	public int getChildCount() throws Exception {
	//		return (int)count(Userlogin.class, "parentId=?", new Object[]{id});
	//	}
	//	public String findChildIds() throws Exception {
	//		String s = "";
	//		List<Integer> ids = new ArrayList<Integer>();
	//		ids.add(id);
	//		while(!ids.isEmpty()){
	//			int id = ids.remove(0);
	//			s += id + ",";
	//			for(Userlogin item: Userlogin.findAll(Userlogin.class, "parentId=?", new Object[]{id})){
	//				ids.add(item.getId());
	//			}
	//		}
	//		if (!s.equals("")){
	//			s = s.substring(0, s.length() - 1);
	//		}
	//		return s;
	//	}
	public void setId(Integer id){
		this.id=id;
	}
	public Integer getId(){
		return id;
	}

	public void setLoginid(String loginid){
		this.loginid=loginid;
	}
	public String getLoginid(){
		return loginid;
	}

	public void setLoginpwd(String loginpwd){
		this.loginpwd=loginpwd;
	}
	public String getLoginpwd(){
		return loginpwd;
	}

	public void setLoginip(String loginip){
		this.loginip=loginip;
	}
	public String getLoginip(){
		return loginip;
	}

	public void setLogindate(Timestamp logindate){
		this.logindate=logindate;
	}
	public Timestamp getLogindate(){
		return logindate;
	}

	public void setActivetime(Timestamp activetime){
		this.activetime=activetime;
	}
	public Timestamp getActivetime(){
		return activetime;
	}

}

